/**
 * @description 数据存储层测试
 * @author AI Assistant
 * @date 2023-10-27
 */

import { storage, STORAGE_KEYS } from './storage'
import { backup } from './backup'
import { listService, groupService, taskService, timerRecordService, noteService } from '@/services'

// 清空测试数据
const clearTestData = () => {
  Object.values(STORAGE_KEYS).forEach(key => {
    storage.remove(key)
  })
  console.log('测试数据已清空')
}

// 测试List服务
const testListService = () => {
  console.log('===== 测试List服务 =====')
  
  // 创建测试
  const list = listService.create({
    title: '测试清单',
    icon: '📝'
  })
  console.log('创建结果:', list)

  // 查询测试
  const lists = listService.getAll()
  console.log('查询结果:', lists)

  // 更新测试
  const updated = listService.update(list.id, {
    title: '更新后的清单'
  })
  console.log('更新结果:', updated)

  return list
}

// 测试Group服务
const testGroupService = (listId) => {
  console.log('===== 测试Group服务 =====')
  
  // 创建测试
  const group1 = groupService.create({
    name: '测试分组1',
    listId: listId,
    order: 0
  })
  console.log('创建结果1:', group1)
  
  const group2 = groupService.create({
    name: '测试分组2',
    listId: listId,
    order: 1
  })
  console.log('创建结果2:', group2)

  // 查询测试
  const groups = groupService.getGroupsByListId(listId)
  console.log('查询结果:', groups)

  // 更新测试
  const updated = groupService.update(group1.id, {
    name: '更新后的分组'
  })
  console.log('更新结果:', updated)
  
  // 排序测试
  const orderUpdated = groupService.updateOrder([group2.id, group1.id])
  console.log('排序结果:', orderUpdated)
  const groupsAfterOrder = groupService.getGroupsByListId(listId)
  console.log('排序后查询结果:', groupsAfterOrder)

  return group1
}

// 测试Task服务
const testTaskService = (groupId) => {
  console.log('===== 测试Task服务 =====')
  
  // 创建测试
  const task = taskService.create({
    title: '测试任务',
    description: '这是一个测试任务',
    priority: 'medium',
    tags: ['测试', '任务'],
    groupId: groupId,
    status: 'todo',
    order: 0
  })
  console.log('创建结果:', task)

  // 查询测试
  const tasks = taskService.getTasksByGroupId(groupId)
  console.log('查询结果:', tasks)

  // 更新测试
  const updated = taskService.update(task.id, {
    title: '更新后的任务'
  })
  console.log('更新结果:', updated)
  
  // 状态更新测试
  const statusUpdated = taskService.updateStatus(task.id, 'doing')
  console.log('状态更新结果:', statusUpdated)

  return task
}

// 测试TimerRecord服务
const testTimerRecordService = (taskId) => {
  console.log('===== 测试TimerRecord服务 =====')
  
  const now = Date.now()
  
  // 创建测试
  const record = timerRecordService.create({
    taskId: taskId,
    type: 'pomodoro',
    startTime: now - 25 * 60 * 1000,
    endTime: now,
    duration: 25 * 60
  })
  console.log('创建结果:', record)

  // 查询测试
  const records = timerRecordService.getRecordsByTaskId(taskId)
  console.log('查询结果:', records)
  
  // 日期范围查询测试
  const todayRecords = timerRecordService.getTodayRecords()
  console.log('今日记录:', todayRecords)

  return record
}

// 测试Note服务
const testNoteService = (taskId, timerId) => {
  console.log('===== 测试Note服务 =====')
  
  // 创建测试
  const note = noteService.create({
    taskId: taskId,
    timerId: timerId,
    content: '这是一个测试笔记'
  })
  console.log('创建结果:', note)

  // 查询测试
  const notesByTask = noteService.getNotesByTaskId(taskId)
  console.log('按任务查询结果:', notesByTask)
  
  const notesByTimer = noteService.getNotesByTimerId(timerId)
  console.log('按计时器查询结果:', notesByTimer)

  return note
}

// 测试备份功能
const testBackup = () => {
  console.log('===== 测试备份功能 =====')
  
  // 创建备份
  const backupData = backup.create()
  console.log('备份数据:', backupData)

  // 清空数据
  clearTestData()
  
  // 验证数据已清空
  const listsAfterClear = listService.getAll()
  console.log('清空后的清单数据:', listsAfterClear)

  // 恢复备份
  const restored = backup.restore(backupData)
  console.log('恢复结果:', restored)
  
  // 验证数据已恢复
  const listsAfterRestore = listService.getAll()
  console.log('恢复后的清单数据:', listsAfterRestore)
}

// 运行所有测试
export const runAllTests = () => {
  try {
    console.log('开始测试数据存储层...')
    
    // 清空测试数据
    clearTestData()
    
    // 运行测试
    const list = testListService()
    const group = testGroupService(list.id)
    const task = testTaskService(group.id)
    const record = testTimerRecordService(task.id)
    const note = testNoteService(task.id, record.id)
    
    // 测试备份功能
    testBackup()
    
    console.log('测试完成！')
    return true
  } catch (e) {
    console.error('测试出错:', e)
    return false
  }
} 